package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Stock;
import com.ruoyi.system.domain.Stocklist;
import com.ruoyi.system.service.IStockService;
import com.ruoyi.system.service.IStocklistService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Screenstock;
import com.ruoyi.system.service.IScreenstockService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 每日股票筛选Controller
 * 
 * @author ruoyi
 * @date 2021-12-31
 */
@Controller
@RequestMapping("/system/screenstock")
public class ScreenstockController extends BaseController
{
    private String prefix = "system/screenstock";

    @Autowired
    private IScreenstockService screenstockService;
    @Autowired
    private IStocklistService stocklistService;

    @RequiresPermissions("system:screenstock:view")
    @GetMapping()
    public String screenstock()
    {
        return prefix + "/screenstock";
    }

    /**
     * 查询每日股票筛选列表
     */
    @RequiresPermissions("system:screenstock:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Screenstock screenstock)
    {
        startPage();
        List<Screenstock> list = screenstockService.selectScreenstockList(screenstock).stream().map(screenstock1 -> {
            screenstock1.setNum(screenstock1.getStocks().split(",").length);
            return screenstock1;
        }).collect(Collectors.toList());
        return getDataTable(list);
    }

    /**
     * 导出每日股票筛选列表
     */
    @RequiresPermissions("system:screenstock:export")
    @Log(title = "每日股票筛选", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Screenstock screenstock)
    {
        List<Screenstock> list = screenstockService.selectScreenstockList(screenstock);
        ExcelUtil<Screenstock> util = new ExcelUtil<Screenstock>(Screenstock.class);
        return util.exportExcel(list, "每日股票筛选数据");
    }

    @GetMapping("/index")
    public String index(Screenstock screenstock, ModelMap model)
    {
//        model.put("date", DateFormatUtils.format(screenstock.getDate(), "yyyy-MM-dd"));
        model.put("id", screenstock.getId());
        return prefix + "/index";
    }

    @RequestMapping("/getList")
    @ResponseBody
    public List<Stocklist> getList(Long id)
    {
        Screenstock screenstock = screenstockService.selectScreenstockById(id);
        List<Stocklist> stocklists=new ArrayList<>();
        for (String stock : screenstock.getStocks().split(",")) {
            stocklists.add(stocklistService.getOne(new Stocklist(stock)));
        }
        return stocklists;
    }

    /**
     * 新增每日股票筛选
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存每日股票筛选
     */
    @RequiresPermissions("system:screenstock:add")
    @Log(title = "每日股票筛选", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Screenstock screenstock)
    {
        return toAjax(screenstockService.insertScreenstock(screenstock));
    }

    /**
     * 修改每日股票筛选
     */
    @RequiresPermissions("system:screenstock:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Screenstock screenstock = screenstockService.selectScreenstockById(id);
        mmap.put("screenstock", screenstock);
        return prefix + "/edit";
    }

    /**
     * 修改保存每日股票筛选
     */
    @RequiresPermissions("system:screenstock:edit")
    @Log(title = "每日股票筛选", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Screenstock screenstock)
    {
        return toAjax(screenstockService.updateScreenstock(screenstock));
    }

    /**
     * 删除每日股票筛选
     */
    @RequiresPermissions("system:screenstock:remove")
    @Log(title = "每日股票筛选", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(screenstockService.deleteScreenstockByIds(ids));
    }
}
